<?php

class App_Model_DbTable_User_Account_Base_Email extends Ext_Db_Table  
{  

    protected $_name = 'user_account_base_email';
    
    private static $_userAccountBaseEmailRow = array();
    
    public function getRowByEmail($email)
    {
        if(!$email)
        {
            return "";
        }
        
        if(isset(self::$_userAccountBaseEmailRow['email'][$email]))
        {
            return self::$_userAccountBaseEmailRow['email'][$email];
        }
        
        $db = $this->getAdapter();
        $safe = $db->quoteInto(" email = ? ", $email);
        $row = $this->fetchRow($safe);
        if (!$row) {
            return "";
        }
        $_userAccountBaseEmailRow = $row->toArray();

        self::$_userAccountBaseEmailRow['email'][$email] = $_userAccountBaseEmailRow;
                
        return $_userAccountBaseEmailRow;
    }
    
    public function addNew($inputArray)
    {
        $sql_data_array = array();
        if(array_key_exists('email', $inputArray)) $sql_data_array['email'] = $inputArray['email'];
        if(array_key_exists('userId', $inputArray)) $sql_data_array['userId'] = $inputArray['userId'];
        if(array_key_exists('status', $inputArray)) $sql_data_array['status'] = $inputArray['status'];
        if(!$sql_data_array)
        {
            return "";
        }
        
        try {
            $rows_affected = $this->insert($sql_data_array);
            $last_insert_id = $this->getAdapter()->lastInsertId();
        }catch (Exception $e){
            trigger_error($e->getMessage(), E_USER_WARNING);
        }
        return $last_insert_id;
    }
    
    public function addOld($inputArray, $email)
    {
        $sql_data_array = array();
        if(array_key_exists('userId', $inputArray)) $sql_data_array['userId'] = $inputArray['userId'];
        if(array_key_exists('status', $inputArray)) $sql_data_array['status'] = $inputArray['status'];
        if(!$sql_data_array)
        {
            return "";
        }
        
        try {
            $db = $this->getAdapter();
            $where = $db->quoteInto(" email = ? ", $email);
            $rows_affected = $this->update($sql_data_array, $where);
            if($rows_affected){
                $this->_unsetEmailRow($email);
            }
        }catch (Exception $e){
            trigger_error($e->getMessage(), E_USER_WARNING);
        }
        
        return "";
    }
    
    private function _unsetEmailRow($email)
    {
        if(!$email)
        {
            return "";
        }
        
        $userAccountBaseEmailRow = $this->getRowByEmail($email);
        if(!$userAccountBaseEmailRow)
        {
            return "";
        }

        unset(self::$_userAccountBaseEmailRow['email'][$email]);
    }
    
}  

